﻿using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEditor;
using UnityEngine;

namespace Learning
{
    public class Simple2JobSystem : MonoBehaviour
    {
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                NativeArray<float> result = new NativeArray<float>(100000000, Allocator.TempJob);

                Simple2Job job = new Simple2Job();
                job.result = result;
                
                var last = EditorApplication.timeSinceStartup;
                
                JobHandle handle = job.Schedule(result.Length, 64);
                handle.Complete();
                
                var now = EditorApplication.timeSinceStartup;
                var pass = now - last;
                Debug.Log("Job耗时：" + pass + "s");
                
                Debug.Log(result[123456]);
                
                result.Dispose();
            }
        }
    }
    
    [BurstCompile] // 注释和打开这里，来查看是否开启Burst编译的差异
    public struct Simple2Job : IJobParallelFor
    {
        public NativeArray<float> result;

        public void Execute(int index)
        {
            result[index] = index * Mathf.PI;
        }
    }
}
